package com.link4a.doms.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {
	protected static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6','7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
	
	protected static MessageDigest messageDigest = null;
	
	static {
		try {
			messageDigest = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException nsaex) {
			System.err.println(MD5Util.class.getName()+"初始化失败，MessageDigest不支持MD5!");
			nsaex.printStackTrace();
		}
	}

	/**
	 * 计算文件的MD5校验码
	 * @param file 文件
	 * @return 32位大写MD5字符串
	 * @throws IOException
	 */
	public static String checksum(File file) throws IOException {
		FileInputStream in = new FileInputStream(file);
		
		try{
			FileChannel ch = in.getChannel();
			
			//700000000 bytes are about 670M
			int maxSize=700000000;
			
			long startPosition=0L;
			long step=file.length()/maxSize;
			
			if(step == 0){
				MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0,file.length());
				messageDigest.update(byteBuffer);
				return bufferToHex(messageDigest.digest());
			}
			
			for(int i=0;i<step;i++){
				MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, startPosition,maxSize);
				messageDigest.update(byteBuffer);
				startPosition+=maxSize;
			}
			
			if(startPosition==file.length()){
				return bufferToHex(messageDigest.digest());
			}

			MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, startPosition,file.length()-startPosition);
			messageDigest.update(byteBuffer);
			
				
			return bufferToHex(messageDigest.digest());
		} finally {
			if (in != null) {
				in.close();
			}
		}
	}

	/**
	 * 采用MD5算法将明文加密
	 * @param s 明文
	 * @return 32位大写MD5字符串
	 */
	public static String encrypt(String s) {
		if (s == null) {
			throw new IllegalArgumentException("argument is null");
		}
		try {
			return encrypt(s.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}
	
	/**
	 * 采用MD5算法将明文加密
	 * @param s 明文
	 * @return 16位大写MD5字符串
	 */
	public static String encrypt16(String s) {
		return encrypt(s).substring(8, 24);
	}

	/**
	 * 采用MD5算法将明文加密
	 * @param bytes 明文
	 * @return 32位大写MD5字符串
	 */
	public static String encrypt(byte[] bytes) {
		messageDigest.update(bytes);
		return bufferToHex(messageDigest.digest());
	}

	private static String bufferToHex(byte bytes[]) {
		return bufferToHex(bytes, 0, bytes.length);
	}

	private static String bufferToHex(byte bytes[], int m, int n) {
		StringBuffer stringbuffer = new StringBuffer(2 * n);
		int k = m + n;
		for (int l = m; l < k; l++) {
			appendHexPair(bytes[l], stringbuffer);
		}
		return stringbuffer.toString();
	}

	private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
		char c0 = hexDigits[(bt & 0xf0) >> 4];
		char c1 = hexDigits[bt & 0xf];
		stringbuffer.append(c0);
		stringbuffer.append(c1);
	}
}

